<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="80%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" :colon="true" label-align="top" :data="formData" labelWidth="100px" :rules="rules">
        <t-form-item label="标题" name="title">
          <t-input clearable v-model="formData.title" placeholder="请输入标题"></t-input>
        </t-form-item>
        <t-form-item name="content" label="内容">
          <tiny-mce-editor :disabled="disabled" v-model="formData.content"></tiny-mce-editor>
        </t-form-item>
        <t-form-item v-if="replyType==='2'" label="状态" name="status">
          <t-select v-model="formData.status" style="width: 390px" clearable :options="dict.type.sys_problem_status"></t-select>
        </t-form-item>
      </t-form>
    </div>
    <template #footer>
      <t-button theme="default" @click="closeDialog">取消</t-button>
      <t-button theme="primary"  @click="onSubmit">确认</t-button>
    </template>
  </t-dialog>
</template>

<script>
import TinyMceEditor from "@/components/TinyMceEditor/index.vue";

export default {
  name: "add-edit-feedback",
  dicts: ['sys_problem_status'],
  components:{
    TinyMceEditor,
  },
  props: ['header', 'objId', 'parentId', 'replyType', 'disabled'],
  data(){
    return {
      dialog:{
        openReply: false,
        header: '',
      },
      drawerVisible: false,
      visible: true,
      loading: false,
      formData:{
        title: '',
        content: '',
        status: '',
      },
      rules:{
        title: [
          { required: true, message: "标题不能为空" }
        ],
        content: [
          { required: true, message: "内容不能为空" }
        ]
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  methods:{
    getObj() {
      this.$api.system.feedback.get(this.objId).then(response => {
        Object.assign(this.formData, response.data)
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.system.feedback.update(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.feedback.add(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
    refreshFeedBack(){
      this.$refs["feedback-reply-index"].getList();
    }
  }
}
</script>

<style scoped>

</style>
